/**
 * 
 */
(function($){
	
	 function is_leap(year) { 
		   return (year%100==0?res=(year%400==0?1:0):res=(year%4==0?1:0));
	 } 
	
	var artHtml='<div class="candler_up">'+
						'<table class="candler_table">'+
						'</table>'+
					'</div>'+
					'<div class="candler_down">'+
				'</div>';
	
	
	function create(target){
		$(target).empty();
		var opts  = $.data(target,'candler').options;
		if(!opts.date){
			var d = new Date()
			opts.date=d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate();
		}
		
		$(target).addClass("candler");
		createArt(target);
	}
	
	function createArt(target){
		 $(target).append(artHtml);
		 createCandeler(target);
	}
	
	function createCandeler(target){
		var table = $(target).find(".candler_table");
		createRow(table,target);
		createOther(table,target);
	}
	
	function createRow(table,target){
		var tr =$("<tr class='canlder_tr_0'></tr>").appendTo(table);
		tr.append("<td class='canlder_td_0_0'>日</td>");
		tr.append("<td class='canlder_td_0_1'>一</td>");
		tr.append("<td class='canlder_td_0_2'>二</td>");
		tr.append("<td class='canlder_td_0_3'>三</td>");
		tr.append("<td class='canlder_td_0_4'>四</td>");
		tr.append("<td class='canlder_td_0_5'>五</td>");
		tr.append("<td class='canlder_td_0_6'>六</td>");
	}
	/**
	 * 根据日期生成日历
	 * 
	 */
	function createOther(table,target){
		// 获取日期对象
		var d = getDate(target);
		// 获取日期的年份，月份，日。
		var year = d.getFullYear();
		var month = d.getMonth()+1;
		var date = d.getDate();
		// alert(year+"年"+month+"月"+date+"日");
		// 获取当前的第一天
		var n1str=new Date(year,month-1,1);
		// 当月第一天星期几
		var firstday=n1str.getDay();
		// 定义每月的日期。
		var m_days=new Array(31,28+is_leap(year),31,30,31,30,31,31,30,31,30,31);
		// 算出需要多少行  
		var tr_str=Math.ceil((m_days[month-1] + firstday)/7); //表格所需要行数
		
		for(var i=0;i<tr_str;i++) {
			var tr = $("<tr class='canlder_tr_'+"+(i+1)+" ></tr>").appendTo(table);
			for(var k=0;k<7;k++){
				idx=i*7+k;
				date_str=idx-firstday+1;
				(date_str<=0 || date_str>m_days[month-1]) ? date_str="" : date_str=idx-firstday+1;
				tr.append("<td class='canlder_d_td' rows='"+(i+1)+"' cel='"+(k+1)+"' value='"+date_str+"'>"+date_str+"</td>");
			}
		}
		 bindEvent(target); 
		 
	}
	
	function bindEvent(target){
		var opts  = $.data(target,'candler').options;
		$(target).find(".canlder_d_td").bind("mouseover",function(){
			$(this).addClass("candlerd_d_td_over");
		});
		$(target).find(".canlder_d_td").bind("mouseout",function(){
			$(this).removeClass("candlerd_d_td_over");
		});
		$(target).find(".canlder_d_td").bind("click",function(){
			var that = $(this);
			var value = getCurDate(target,that);
			if(that.hasClass("candlerd_d_td_checked")){
				$(this).removeClass("candlerd_d_td_checked");
			}else{
				var val = that.attr("value");
				if(isBigToday(target,val)){
					$(this).addClass("candlerd_d_td_checked");
				}
			}
		});
		
		$(target).find(".canlder_d_td").bind("dblclick",function(){
			var that = $(this);
			var value = getCurDate(target,that);
			opts.onDbClick.call(target,that,value);
		});
	}
	
	
	function getCurDate(target,that){
		var opts  = $.data(target,'candler').options;
		var d = opts.date;
		if(d){
			var ay = d.split("-");
			var val = that.attr("value");
			return  ay[0]+"-"+ay[1]+"-"+val;
		}else{
			d =new Date();
			var year = d.getFullYear();
			var month = d.getMonth()+1;
			var date = d.getDate();
			return year+"-"+month+"-"+val;
		}
	}

	
	function getDate(target){
		var opts  = $.data(target,'candler').options;
		var d = opts.date;
		if(d){
			var ay = d.split("-");
			return  new Date(ay[0],ay[1]-1,ay[2]);
		}
		return new Date();
	}
	
	// 比较是否比今天大
	function isBigToday(target,d1,exclude){
		var opts  = $.data(target,'candler').options;
		var d = opts.date;
		var ay = d.split("-");
		var _y= parseInt(ay[0]);
		var _m = parseInt(ay[1]);
		var _d = parseInt(d1);
		d = new Date();
		var y = d.getFullYear();
		var m = d.getMonth()+1;
		var d = d.getDate();
		// 是否比今天大,是否比 开始时间大
		if(_y>y ){
			return true;
		}
		if(_m>m ){
			return true;
		}
		if(_d>=d){
			return true;
		}
		return false;
	}
	
	
	// 比较是否比今天大
	function isBig(target,d1,exclude){
		
		var opts  = $.data(target,'candler').options;
		var d = opts.date;
		if(!exclude){
			d = new Date();
			var y = d.getFullYear();
			var m = d.getMonth()+1;
			var d = d.getDate();
			exclude=y+'-'+m+'-'+d;
		}
		var sy = exclude.split("-");
		var _sy= parseInt(sy[0]);
		var _sm = parseInt(sy[1]);
		var _sd = parseInt(sy[2]);
		// 日历日期
		var ay = d.split("-");
		var _y= parseInt(ay[0]);
		var _m = parseInt(ay[1]);
		var _d = parseInt(d1);
		// 是否比 开始时间大
		// 是否比今天大,是否比 开始时间大
		if( _y>_sy){
			return true;
		}
		if(_m>_sm){
			return true;
		}
		if( _d >= _sd){
			return true;
		}
		return false;
	}
	
	/**
	 * 根据策略生成订单
	 * 全天，单号，双号，工作日
	 */
	function setValueByCl(target,params,exclude){
		$(target).find(".canlder_d_td").each(function(){
			$(this).removeClass("candlerd_d_td_checked");
		});
		if(params=="qt"){
			setAllDay(target,exclude);
		}
		if(params=="dh"){
			setDhDay(target,exclude);
		}
		if(params=="sh"){
			setShDay(target,exclude);
		}
		if(params=="gzr"){
			setGzrDay(target,exclude);
		}
		if(params=="dhgzr"){
			setDhGzrDay(target,exclude);
		}
		
		if(params=="shgzr"){
			setShGzrDay(target,exclude);
		}
		if(params=="lbt"){
			setLbtDay(target,exclude);
		}
	}
	
	function setAllDay(target,exclude){
		$(target).find(".canlder_d_td").each(function(){
			var that = $(this);
			var val = that.attr("value");
			if(val!="" && isBig(target,val,exclude)){
				$(this).addClass("candlerd_d_td_checked");	
			}
			
		});
	}
	// 单号
	function setDhDay(target,exclude){
		$(target).find(".canlder_d_td").each(function(){
			var that = $(this);
			var val = that.attr("value");
			if(!(val%2==0) && isBig(target,val,exclude)){
				$(this).addClass("candlerd_d_td_checked");
			}
		});
	}
	// 双号
	function setShDay(target,exclude){
		$(target).find(".canlder_d_td").each(function(){
			var that = $(this);
			var val = that.attr("value");
			var col = that.attr("cel");
			if(val!="" && val%2 == 0 && isBig(target,val,exclude) ){
				$(this).addClass("candlerd_d_td_checked");
			}
		});
	}
	
	// 工作日
	function setGzrDay(target,exclude){
		$(target).find(".canlder_d_td").each(function(){
			var that = $(this);
			var val = that.attr("value");
			var col = that.attr("cel");
			if(val!="" && col!="1" && col!="7" && isBig(target,val,exclude)){
				$(this).addClass("candlerd_d_td_checked");
			}
		});
	}
	
	// 单号工作日
	function setDhGzrDay(target,exclude){
		$(target).find(".canlder_d_td").each(function(){
			var that = $(this);
			var val = that.attr("value");
			var col = that.attr("cel");
			if(!(val%2==0) && val!="" && col!="1" && col!="7" && isBig(target,val,exclude) ){
				$(this).addClass("candlerd_d_td_checked");
			}
		});
	}
	// 双号工作日
	function setShGzrDay(target,exclude){
		$(target).find(".canlder_d_td").each(function(){
			var that = $(this);
			var val = that.attr("value");
			var col = that.attr("cel");
			if(  val!="" && val%2 == 0 && col!="1" && col!="7" && isBig(target,val,exclude) ){
				$(this).addClass("candlerd_d_td_checked");
			}
		});
	}
	
	function setLbtDay(target,exclude){
		$(target).find(".canlder_d_td").each(function(){
			var that = $(this);
			var val = that.attr("value");
			var col = that.attr("cel");
			if(  val!="" && (col =="1" || col =="7" ) && isBig(target,val,exclude)){
				$(this).addClass("candlerd_d_td_checked");
			}
		});
	}
	
	$.fn.candler=function(options,params){
		if(typeof options == "string"){
			return $.fn.candler.methods[options](this,params);
		}
		options = options||{};
		return this.each(function(){
			var state= $.data(this,'candler');
			if(state){
				$.extend(state.options, options);
			}else{
				$.data(this, 'candler', {
					options: $.extend({}, $.fn.candler.defaults,$.fn.candler.parseOptions(this),options)
				});
			}
			create(this);
		});
	}
	
	$.fn.candler.defaults={
		 date:"",
		 onDbClick:function(){
		 }
	};
	
	$.fn.candler.parseOptions=function(target,options){
		return {}
	};
	$.fn.candler.methods={
			/**
			 * 全天，单号，双号，工作日
			 * @param jq
			 * @param params
			 */
			setValueByCl:function(jq,params){
				var cScl = params.cScl;
				var excludeData= params.excludeData
				setValueByCl(jq[0],cScl,excludeData);
			},
			/**
			 * 全天，单号，双号，工作日
			 * @param  getSelectedDate
			 * @param params
			 */
			getSelectedDate:function(jq,params){
				var opts = $.data(jq[0],"candler").options;
				var d = opts.date;
				var ay = d.split("-");
				var _y= ay[0];
				var _m = ay[1];
				var ady=[];
				var vs;
				jq.find(".candlerd_d_td_checked").each(function(){
					var that = $(this);
					var val = that.attr("value");
					vs=_y+"-"+_m+"-"+val;
					ady.push(vs);
				});
				return ady.join(";");
			}
	
	
	}
})(jQuery);